草庐IT

php - 比 in_array 快?

全部标签

javascript - 不能将 String.prototype.match 用作 Array.some 的函数吗?

这行不通:vars='^foo';console.log(['boot','foot'].some(s.match));UncaughtTypeError:String.prototype.matchcalledonnullorundefined但是这样做:vars='^foo';console.log(['boot','foot'].some(function(i){returni.match(s)}));这是为什么?我以某种方式想象String.prototype.match函数太“原始”之类的,但究竟是为什么呢?因为我没有使用ES2015,所以第二个版本看起来很冗长。有替代方案吗

javascript - 属性检测 : Using 'in' versus trying to access property

不得不提:我知道一点JavaScript,但我不是很深入。一直认为这是检查对象上的属性是否可用的正确方法:if(window.console){//doSomething}昨天我看到了使用这种技术的代码:if('console'inwindow){//doSomething}这两种技术是否等同?还是他们有区别? 最佳答案 没有。他们有区别。第一个检查window.console的值是否为Truthy,第二个检查window中是否存在console属性。假设您创建了一个这样的变量。window.myName="";现在,if(wind

javascript - 如何解决错误 : "undefined is null or not object ie in ext js" in IE?

我认为这是尾随逗号或语法错误、未声明使用变量的原因。我的js字段是1000行od代码。由于错误没有向我提供行号。它变得难以调试。请帮助我调试IE的技术。该脚本适用于Firefox、Safari。 最佳答案 我会jslint文件。这会发现问题以及您可能遇到的任何其他问题。您可以将其作为commandlineutilityvianode运行. 关于javascript-如何解决错误:"undefinedisnullornotobjectieinextjs"inIE?,我们在StackOver

javascript - 为什么对 Array 原型(prototype)的这种更改在我的 jQuery 插件中不起作用?

我已将以下方法添加到Array原型(prototype)中:Array.prototype.foreach=function(func){for(vari=0;i在同一个文件中,在上面的代码之后,我有以下jQuery插件:jQuery.fn.addClassForEvents=function(){varthat=this;arguments.foreach(function(event){that.bind(event[0],function(){that.addClass(event[0]);}).bind(event[1],function(){that.removeClass(

javascript - Angular : Variations in a template based on a attribute

假设我在AngularJS网络应用程序中有一个Controller,它有一个数据数组,用于存储非常相似但需要不同模板的对象,具体取决于成员变量“类型”。例如:functionfooCtrl($scope){$scope.bar=[{"name":"example1","type":"egType1","text":"Someexampletext"},{"name":"example2","type":"egType2","text":"Someexampletext"},{"name":"example3","type":"egType3","text":"Someexamplete

javascript - 对于 Array,在 javascript 中使用 map() 和 reduce() 而不是 forEach() 效率更高吗?

1)正如我们所知,map()和reduce()没有副作用。如今,我们的手机也有多核。那么使用它们效率更高吗?2)另一方面,js在大多数浏览器上只有一个线程可以执行。因此map()和reduce()是为服务器端脚本准备的? 最佳答案 我今天刚刚测试了这个,使用map和reduce处理float,使用最新的node.js版本,答案是map和reduce比常规的for循环慢两个数量级。varr=array.map(x=>x*x).reduce((total,num)=>total+num,0);~11,000毫秒varr=0.0;arra

javascript - D3 过渡 : Fading in and out the colors within a gradient fill

在这个D3图中,圆圈填充了径向渐变,并且改变不透明度用于淡入和淡出:varwidth=400,height=400,padding=1.5,//separationbetweensame-colornodesclusterPadding=6,//separationbetweendifferent-colornodesmaxRadius=12;varn=200,//totalnumberofnodesm=10;//numberofdistinctclustersvarcolor=d3.scale.category10().domain(d3.range(m));//Thelargest

javascript - npm 脚本 : need to minify all HTML files in folder (and subfolders)

我想使用npmrun脚本缩小文件夹(以及其中的任何文件夹)中的所有.html文件。理想情况下,应覆盖所有.html文件(如果不可能,也可以使用新文件夹)。假定输入文件夹中会有非HTML文件。npmlibraryminimize仅适用于每个文件,但不适用于文件夹。另一个npm库html-minifier确实接受文件夹作为输入,但如果输入文件夹中存在任何非HTML文件,则会失败:html-minifier--input-dir./test1--output-dir./test2--html-5--collapse-whitespace我需要它来缩小我的静态网站的HTML文件。

javascript - "TypeError: this is not a typed array.with"Node.js 中的 WS.js

我的服务器文件中只有这个,出现错误:constWebSocket=require('ws');constwss=newWebSocket.Server({port:9000});wss.broadcast=functionbroadcast(data){wss.clients.forEach(functioneach(client){if(client.readyState===WebSocket.OPEN){client.send(data);}});};wss.on('connection',functionconnection(ws){ws.on('message',functi

javascript - Array.indexOf 不敏感数据类型

我一直在GoogleChrome控制台中使用Array.indexOf,我尝试了这些代码[1,2,3].indexOf(3);[1,2,"3"].indexOf("3");他们都返回了2,但是当我尝试这些代码时[1,2,"3"].indexOf(3);[1,2,3].indexOf("3");他们都返回-1。我希望它也返回2,我该怎么做?感谢您的帮助、时间和精力! 最佳答案 扩展guest271314的帖子:将两个值都转换为字符串。这也适用于数字和字符串val=trueconsole.log([1,2,"true"].findInd